package com.masterclient.app.modules.firstpager.fragments.adapters;

import android.content.Context;
import android.graphics.Paint;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.R;
import com.eoner.baselibrary.bean.firstpager.RecommendPagerBean;
import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.baselibrary.utils.ScreenUtils;

import java.util.List;


/**
 * 作者：sean
 * 创建时间：2017/6/13 16:11
 * 功能说明：首页推荐新品特惠viewpager中的view
 */

public class NewGoodsPromotionPagerAdapter extends PagerAdapter {
    private Context context;
    private List<RecommendPagerBean.DataBean.ShNewProductBean.ShProductsBeanX> list;

    public  NewGoodsPromotionPagerAdapter(Context context, List<RecommendPagerBean.DataBean.ShNewProductBean.ShProductsBeanX> list) {
        this.list = list;
        this.context = context;
    }


    private int mChildCount = 0;

    @Override
    public void notifyDataSetChanged() {
        mChildCount = getCount();
        super.notifyDataSetChanged();
    }
    @Override
    public int getItemPosition(Object object)   {
        if ( mChildCount > 0) {
            mChildCount --;
            return POSITION_NONE;
        }
        return super.getItemPosition(object);
    }


    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        RecommendPagerBean.DataBean.ShNewProductBean.ShProductsBeanX beanX = list.get(position);
        View itemView = LayoutInflater.from(context).inflate(R.layout.view_new_goods, null);
        ImageView ivNewGoods = (ImageView) itemView.findViewById(R.id.iv_new_goods);
        TextView tvGoodsName = (TextView) itemView.findViewById(R.id.tv_goods_name);
        TextView tvGoodsSubname = (TextView) itemView.findViewById(R.id.tv_goods_subname);
        TextView tvGoodsDesc = (TextView) itemView.findViewById(R.id.tv_goods_desc);
        TextView tvPromotionPrice = (TextView) itemView.findViewById(R.id.tv_promotion_price);
        TextView tvOriginalPrice = (TextView) itemView.findViewById(R.id.tv_original_price);


        LinearLayout ll_user = (LinearLayout) itemView.findViewById(R.id.ll_user);
        LinearLayout ll_earn = (LinearLayout) itemView.findViewById(R.id.ll_earn);
        TextView tv_price_n = (TextView) itemView.findViewById(R.id.tv_price_n);
        TextView tv_money_earn = (TextView) itemView.findViewById(R.id.tv_money_earn);
        if(0 == Double.valueOf(beanX.getSh_partner_commission())){
            ll_earn.setVisibility(View.GONE);
            ll_user.setVisibility(View.VISIBLE);
        }else {
            ll_earn.setVisibility(View.VISIBLE);
            ll_user.setVisibility(View.GONE);
            tv_price_n.setText("¥"+beanX.getSh_show_price());
            tv_money_earn.setText("赚"+beanX.getSh_partner_commission());
        }


        GlideUtil.displayRoundImage(context,beanX.getSh_image(),ivNewGoods,R.mipmap.defaults,ScreenUtils.dp2px(14),ScreenUtils.dp2px(165),ScreenUtils.dp2px(165));
//        App.picasso
//                .load(beanX.getSh_image())
//                .resize(ScreenUtils.dp2px(165),ScreenUtils.dp2px(165))
//                .placeholder(R.mipmap.defaults)
//                .error(R.mipmap.defaults)
//                .transform(new PicassoRoundTransform())
//                .into(ivNewGoods);

        tvGoodsName.setText(beanX.getSh_name());
        tvGoodsSubname.setText(beanX.getSh_subname());
        if(TextUtils.isEmpty(beanX.getSh_description())){
            tvGoodsDesc.setVisibility(View.GONE);
        }else {
            tvGoodsDesc.setText(beanX.getSh_description());
        }
        tvOriginalPrice.setText("¥"+beanX.getSh_line_price());
        tvOriginalPrice.setPaintFlags(Paint. STRIKE_THRU_TEXT_FLAG| Paint.ANTI_ALIAS_FLAG);
        tvPromotionPrice.setText("¥"+beanX.getSh_show_price());
        container.addView(itemView);
        itemView.setOnClickListener(v -> {
            ARouter.getInstance().build(AroutePath.COMMODITY_DETAIL_ACTIVITY)
                    .withString("id", beanX.getSh_id())
                    .navigation();
        });
        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(((View) object));
    }

    public void setList(List<RecommendPagerBean.DataBean.ShNewProductBean.ShProductsBeanX> list) {
        this.list = list;
        notifyDataSetChanged();
    }

}
